using FreeSql.DataAnnotations;
using SumerCoreDevOps.Shared.Enums;

namespace SumerCoreDevOps.Domain.Entities;

/// <summary>
/// 任务实体
/// </summary>
[Table(Name = "tasks")]
[Index("idx_instance", nameof(PipelineInstanceId), false)]
[Index("idx_node", nameof(NodeId), false)]
[Index("idx_status", nameof(Status), false)]
public class TaskEntity
{
    [Column(IsPrimary = true, IsIdentity = true)]
    public long Id { get; set; }

    /// <summary>
    /// 流水线实例ID
    /// </summary>
    public long PipelineInstanceId { get; set; }

    /// <summary>
    /// 步骤ID
    /// </summary>
    public long StepId { get; set; }

    /// <summary>
    /// 执行节点ID
    /// </summary>
    public long? NodeId { get; set; }

    /// <summary>
    /// 任务状态
    /// </summary>
    public Shared.Enums.TaskStatus Status { get; set; } = Shared.Enums.TaskStatus.Pending;

    /// <summary>
    /// 进度（0-100）
    /// </summary>
    public int Progress { get; set; }

    /// <summary>
    /// 错误信息
    /// </summary>
    [Column(StringLength = -1)]
    public string? ErrorMessage { get; set; }

    /// <summary>
    /// 日志路径
    /// </summary>
    [Column(StringLength = 500)]
    public string? LogPath { get; set; }

    /// <summary>
    /// 开始时间
    /// </summary>
    public DateTime? StartTime { get; set; }

    /// <summary>
    /// 结束时间
    /// </summary>
    public DateTime? EndTime { get; set; }

    /// <summary>
    /// 创建时间
    /// </summary>
    public DateTime CreatedAt { get; set; } = DateTime.UtcNow;

    /// <summary>
    /// 输出上下文（JSON格式）
    /// </summary>
    [Column(StringLength = -1)]
    public string? OutputContext { get; set; }

    /// <summary>
    /// 流水线实例
    /// </summary>
    [Navigate(nameof(PipelineInstanceId))]
    public PipelineInstance PipelineInstance { get; set; } = null!;

    /// <summary>
    /// 执行节点
    /// </summary>
    [Navigate(nameof(NodeId))]
    public NodeStatus? Node { get; set; }
}
